//
//  MCTextAutoFillService.m
//  MirkoLibTest
//
//  Created by Mirko on 2017/3/21.
//  Copyright © 2017年 Mirko. All rights reserved.
//

#import "MCTextAutoFillService.h"
#import "MCViewAutoFill.h"
#import "UIView+MCAutoFill.h"
#import "MCTableViewAutoFill.h"
#import <objc/runtime.h>

static const NSString *kOverWriteDisappearKey = @"overwrite_disappear_key";

@interface MCTextAutoFillService ()

//存放需要填充的数据
@property (nonatomic, strong) MCTableViewAutoFill *tableFill;
@property (nonatomic, strong) MCViewAutoFill *viewFill;

@end

@implementation MCTextAutoFillService

-(instancetype) init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

//-(void) overWriteDisappear:(UIViewController *) controller
//{
//    NSLog(@"start");
//    Method sys = class_getInstanceMethod([controller class], @selector(viewDidDisappear:));
//    Method own = class_getInstanceMethod([self class], @selector(mc_viewDidDisappear:));
//    class_addMethod([controller class], @selector(mc_viewDidDisappear:), method_getImplementation(own), method_getTypeEncoding(own));
//    Method newOwn = class_getInstanceMethod([controller class], @selector(mc_viewDidDisappear:));
//    method_exchangeImplementations(sys, newOwn);
//    objc_setAssociatedObject(controller, &kOverWriteDisappearKey, self, OBJC_ASSOCIATION_ASSIGN);
//    NSLog(@"end");
//    NSLog(@"");
//}
//
//-(void) mc_viewDidDisappear:(BOOL)animated
//{
//    MCTextAutoFillService *service = objc_getAssociatedObject(self, &kOverWriteDisappearKey);
//    [service mc_viewDidDisappear:animated];
//    
//    NSLog(@"suss exchange!");
//    
//}

-(void) addAutoFillItem:(UIViewController *) controller rootView:(UIView *) view
{
    if (![controller conformsToProtocol:(@protocol(MCTextAutoFillDelegate))]) {
        NSLog(@"controller is not implement protocol MCTextAutoFillDelegate");
        return;
    };
    
    id <MCTextAutoFillDelegate> delegate = (id <MCTextAutoFillDelegate>)controller;
    
    self.viewFill = [[MCViewAutoFill alloc] initWithController:controller];
    NSArray *fillData = [delegate autoFillData:controller];
    [self.viewFill autoFillWithDatas:fillData];
//    [self overWriteDisappear:controller];
}

-(void) addAutoFillItem:(UIViewController *) controller tablevView:(UITableView *) view
{
    if (![controller conformsToProtocol:(@protocol(MCTextAutoFillDelegate))]) {
        NSLog(@"controller is not implement protocol MCTextAutoFillDelegate");
        return;
    };
    if (!self.tableFill) {
        self.tableFill = [[MCTableViewAutoFill alloc] initWithController:controller];
    }
    id <MCTextAutoFillDelegate> delegate = (id <MCTextAutoFillDelegate>)controller;
    NSArray *fillData = [delegate autoFillData:controller];
    [self.tableFill.autoFillDatas addObjectsFromArray:fillData];
    [view reloadData];
//    [self overWriteDisappear:controller];
}

-(void) setValue:(NSString *) string index:(NSInteger) viewIdx controller:(UIViewController *) controller
{
    [self.viewFill autoFillItem:string index:viewIdx];
}

-(void) removeAutoFillItem:(UIViewController *) controller
{
//    [self.viewList removeAllObjects];
    
    [self.tableFill removeAutoFill];
    
    NSLog(@"");
}

-(void) dealloc
{
    NSLog(@"%s%@", __PRETTY_FUNCTION__, NSStringFromSelector(_cmd));
}

@end
